﻿using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.BLL
{
    public class UserManager : BaseManager
    {
        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static OperateResult Save(tb_User user)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOftb_User result = new ResultHelperOftb_User();
                if (user.Id == 0)
                {
                    user.Subtime = DateTime.Now;
                    result = service.Adduser(user);
                }
                else
                {
                    result = service.Detailuser(user.Id);
                    if (result.S)
                    {
                        tb_User oldUser = result.D;
                        oldUser.UserId = user.UserId;
                        oldUser.UserName = user.UserName;
                        oldUser.Password = user.Password;
                        oldUser.DepartmentCode = user.DepartmentCode;
                        oldUser.DepartmentName = user.DepartmentName;
                        oldUser.UserType = user.UserType;
                        oldUser.JobCode = user.JobCode;
                        oldUser.JobTitle = user.JobTitle;
                        oldUser.PositionCode = user.PositionCode;
                        oldUser.PositionTitle = user.PositionTitle;
                        oldUser.Sex = user.Sex;
                        oldUser.ICCode = user.ICCode;
                        oldUser.BarCode = user.BarCode;
                        oldUser.Telephone = user.Telephone;
                        oldUser.Birthday = user.Birthday;
                        oldUser.Major = user.Major;
                        oldUser.Remarks = user.Remarks;
                        oldUser.PowerRole = user.PowerRole;
                        oldUser.RoleName = user.RoleName;
                        oldUser.Status = user.Status;
                        result = service.updateuser(oldUser); 
                    }
                }
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult Delete(int id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfInt32 result = service.Deluser(id.ToString());
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult GetUser(int id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOftb_User result = service.Detailuser(id);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        public static OperateResult EditPwdSave(string pwd,string newPwd)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                tb_User user = new tb_User();
                SysLoginUser loginUser = LoginManager.GetLoginUser();
                resultInfo = GetUser(loginUser.Id);
                if (resultInfo.Result == 1)
                {
                    user = resultInfo.Body as tb_User;
                }
                if (!user.Password.Equals(pwd))
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = "密码不正确";
                }
                if (resultInfo.Result == 1)
                {
                    user.Password = newPwd;
                    resultInfo = Save(user);
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}
